package com.sun.controller;

import com.sun.dao.EmployeeDao;
import com.sun.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.util.Collection;

@Controller
public class AjaxController {

    @Autowired
    EmployeeDao employeeDao;

    /**
     * 将返回的数据放在响应体中
     * @return employee集合
     */
    @ResponseBody
    @RequestMapping("/empsAjax")
    public Collection<Employee> ajaxGetAll(){
        return employeeDao.getAll();
    }

    /**
     * requestBody：请求体
     * @param string 请求体文本
     */
    @RequestMapping("/requestBody")
    public String testRequestBody(@RequestBody String string){
        System.out.println(string);
        return "hello";
    }

    /**
     * 获取一个json形式的请求
     */
    @RequestMapping("/requestJson")
    public String requestJson(@RequestBody Employee employee){
        System.out.println(employee);
        return "hello";
    }

    @RequestMapping("/httpEntity")
    public String test01(HttpEntity<String> string){
        System.out.println("http实体"+string);
        return "hello";
    }

    @RequestMapping("/responseEntity")
    public ResponseEntity<String> test02(){
        System.out.println("http实体");
        String body ="<h1>success</h1>";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Set-Cookie","username=hahaha");

        return new ResponseEntity<String>(body,httpHeaders, HttpStatus.OK);
    }

}
